package labrom.usage;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;


public class UsageDataServiceConnection implements ServiceConnection {
	
	private UsageDataService localService;
	private boolean isBound;
	private Listener listener;
	
	public interface Listener {
		void serviceConnected();
	}
	
	
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the service object we can use to
        // interact with the service.  Because we have bound to a explicit
        // service that we know is running in our own process, we can
        // cast its IBinder to a concrete class and directly access it.
        localService = ((UsageDataService.LocalBinder)service).getService();
        isBound = true;
        if(listener != null)
        	listener.serviceConnected();
    }

    public void onServiceDisconnected(ComponentName className) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        // Because it is running in our same process, we should never
        // see this happen.
        localService = null;
        isBound = false;
    }
    
    public UsageDataService getLocalService() {
		return localService;
	}
    
    public boolean isBound() {
    	if(!isBound)
    		Log.w(D.TAG, "Usage Data service is not bound!");
		return isBound;
	}

	public Listener getListener() {
		return listener;
	}

	public void setListener(Listener listener) {
		this.listener = listener;
	}
    
    
}
